//
//  HACStockTimeAxis.m
//  HStockCharts
//
//  Created by Hotacool on 2017/12/28.
//

#import "HACStockTimeAxis.h"

static CGFloat const timeLabelWidth = 100;
@implementation HACStockTimeAxis {
    NSMutableArray <UILabel *> *_timeLabels;
    int _minSum;
    NSMutableArray <NSNumber*> *_minArray;
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setUp];
    }
    return self;
}

- (void)setUp {
    _timeLabels = [NSMutableArray arrayWithCapacity:2];
    _minSum = 0;
    _minArray = [NSMutableArray arrayWithCapacity:2];
}

- (void)setTimeArray:(NSArray *)timeArray {
    _timeArray = timeArray;
    if (timeArray&&timeArray.count>0) {
        [_timeLabels enumerateObjectsUsingBlock:^(UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            [obj removeFromSuperview];
        }];
        [_timeLabels removeAllObjects];
        [_minArray removeAllObjects];
        NSUInteger count = timeArray.count;
        int __block s = 0, e = 0, sum = 0;
        [timeArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            UILabel *__block label;
            if (idx%2 == 0) {
                s = [self minCountFromTimeString:obj];
                NSString *tmp;
                if (idx == 0) {
                    tmp = [self formatTimeString:obj];
                } else {
                    tmp = [NSString stringWithFormat:@"%@/%@", [self formatTimeString:timeArray[idx-1]], [self formatTimeString:obj]];
                }
                label = [self createTimeLableWithTitle:tmp];
            } else {
                if (idx == count - 1) {
                    label = [self createTimeLableWithTitle:[self formatTimeString:obj]];
                }
                e = [self minCountFromTimeString:obj];
                int l = e - s;
                [_minArray addObject:@(l)];
                sum += l;
                s = 0;
                e = 0;
            }
            if (label) {
                [self addSubview:label];
                [_timeLabels addObject:label];
            }
        }];
        _minSum = sum;
    }
}

- (void)refreshUIWithTimeArray:(NSArray *)timeArray {
    self.timeArray = timeArray;
    [self refreshUI];
}

- (void)refreshUI {
    [self setNeedsLayout];
    [self setNeedsDisplay];
}

- (NSString *)titleForTimeAtIndex:(NSUInteger)index {
    return @"";
}

- (void)layoutSubviews {
    if (_minSum <= 0) {
        return;
    }
    CGSize size = self.frame.size;
    int count = (int)_timeLabels.count;
    [_timeLabels enumerateObjectsUsingBlock:^(UILabel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if (idx == 0) {
            obj.frame = CGRectMake(0, 0, timeLabelWidth, size.height);
            obj.textAlignment = NSTextAlignmentLeft;
        } else if (idx == count-1) {
            obj.frame = CGRectMake(size.width - timeLabelWidth, 0, timeLabelWidth, size.height);
            obj.textAlignment = NSTextAlignmentRight;
        } else {
            CGFloat marginX = ([_minArray[idx-1] floatValue]/_minSum) * size.width - timeLabelWidth/2;
            obj.frame = CGRectMake(marginX, 0, timeLabelWidth, size.height);
            obj.textAlignment = NSTextAlignmentCenter;
        }
    }];
}

- (int)minCountFromTimeString:(NSString*)timeString {
    if (!timeString || timeString.length == 0) {
        return 0;
    }
    int minCount = 0;
    int hCount = [[timeString substringWithRange:NSMakeRange(0, 2)] intValue] * 60;
    int mCount = [[timeString substringWithRange:NSMakeRange(2, 2)] intValue];
    minCount = hCount + mCount;
    return minCount;
}

- (NSString*)formatTimeString:(NSString*)timeString {
    if (!timeString || timeString.length == 0) {
        return 0;
    }
    NSString *ret;
    int hCount = [[timeString substringWithRange:NSMakeRange(0, 2)] intValue];
    int mCount = [[timeString substringWithRange:NSMakeRange(2, 2)] intValue];
    ret = [NSString stringWithFormat:@"%d:%@", hCount, mCount==0?@"00":@(mCount)];
    return ret;
}

- (UILabel*)createTimeLableWithTitle:(NSString*)title {
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
    label.font = [UIFont systemFontOfSize:14];
    label.text = title;
    label.textColor = [UIColor blackColor];
    return label;
}
@end
